package com.dongdongshop.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.service.SmsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@RocketMQMessageListener(consumerGroup = "dk2206",topic = "sms")
public class MyConsumer implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @DubboReference
    private SmsService smsService;

    @Override
    public void onMessage(String message) {
        System.out.println("消费成功" + message);
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                MessageExt messageExt = list.get(0);
                int reconsumeTimes = messageExt.getReconsumeTimes();
                try {
                    messageExt.getTopic();
                    String msgId = messageExt.getMsgId();
                    Boolean key = stringRedisTemplate.hasKey("MESSAGE_SMS_" + msgId);
                    if (!key){
                        byte[] body = messageExt.getBody();
                        String s = new String(body);
                        System.out.println(s);
                        smsService.sendPhone(s);
                        stringRedisTemplate.opsForHash().put("MESSAGE_SMS","MESSAGE_SMS_" + msgId,msgId+"消息已消费");
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;//消费成功
                } catch (Exception e) {
                    e.printStackTrace();
                    if (reconsumeTimes < 2 ){
                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;//稍后重试
                    }
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;//消费成功
                }
            }
        });

    }
}
